package edu.ncsu.csc.ip.client.run;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import edu.ncsu.csc.ip.client.PeerClient;
import edu.ncsu.csc.ip.client.protocol.ProtocolClientImpl;

public class Task2Thread extends Thread {

	private PeerClient peerClient;
	int[] rfcFileList;
	String name;

	public Task2Thread(String name, PeerClient peerClient, int[] rfcFileList) {
		super(name);
		this.peerClient = peerClient;
		this.rfcFileList = rfcFileList;
		this.name = name;
	}

	@Override
	public void run() {

		long time = 0;
		long accumulation = 0;
		try {
			File file = new File("./Task2_" + name + "_results.csv");
			PrintWriter printWrite = new PrintWriter(new FileWriter(file,
					false));
			printWrite.println("Order , RFC#, Time, Cumulative Time");
			for (int i = 0; i < rfcFileList.length; i++) {

				time = peerClient.runCommand(ProtocolClientImpl.GETRFC + " "
						+ rfcFileList[i]);
				accumulation += time;
				printWrite.println(i + "," +  rfcFileList[i]
						+ "," + time + "," + accumulation);

			}

			printWrite.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
